from splinter.browser import Browser
from time import sleep
import traceback

class Reserve_court(object):
    def __init__(self,username,password,which_court,when_to_start,during_time):
        self.username=username
        self.passwd=password
        self.login_url="https://cgyy.buaa.edu.cn/venue-server/sso/manageLogin"
        self.during_time=during_time
        self.when_to_start=when_to_start
        self.which_court=which_court

    def login(self):
        self.driver.visit(self.login_url)
        sleep(1)
        with self.driver.get_iframe("loginIframe") as iframe:
            self.driver.find_by_id("unPassword").fill(self.username)
            self.driver.find_by_id('pwPassword').fill(self.passwd)
            self.driver.find_by_xpath('//*[@id="content-con"]/div[1]/div[7]').click()
            sleep(1)
            print("输入了账号和密码")
        #  检验是否有弱密码忽略界面
            if self.driver.url=="https://sso.buaa.edu.cn/login":
                button_ignore=self.driver.find_by_value("忽略一次").click()
            for i in range(10):
                if self.driver.url!="https://cgyy.buaa.edu.cn/venue/home":
                    sleep(1)
                else:
                    # print("登录成功")
                    return 1
            print("登陆过于频繁，访问被拒绝了")
            return 0



    def start_reserve(self):
        self.driver=Browser("chrome")
        if self.login():
            print("登陆成功")
            # 进入登陆状态后，访问你想要预定的场馆地址即可
            self.driver.visit("https://cgyy.buaa.edu.cn/venue/venue-reservation/57")
            self.driver.find_by_xpath(
                "/html/body/div[1]/div/div/div[3]/div[2]/div/div[2]/form/div/div/button[2]").click()
            self.driver.find_by_xpath(
                "/html/body/div[1]/div/div/div[3]/div[2]/div/div[2]/form/div/div/button[2]").click()

            # 现在已经来到两天后的预定场地
            # //*[@id="scrollTable"]/div/table/tbody/tr[8]/td[2]
            # //*[@id="scrollTable"]/div/table/tbody/tr[8]/td[2]
            #         //*[@id="scrollTable"]/div/table/tbody/tr[8]/td[3]/div
            #         //*[@id="scrollTable"]/div/table/tbody/tr[8]/td[3]/div
            # //*[@id="scrollTable"]/div/table/tbody/tr[8]/td[4]/div
            #         //*[@id="scrollTable"]/div/table/thead/tr/td[6]/div/span/i
            #         //*[@id="scrollTable"]/div/table/thead/tr/td[6]/div/span/i
            #         //*[@id="scrollTable"]/div/table/tbody/tr[8]/td[1]
            #         // *[ @ id = "scrollTable"] / div / table / tbody / tr[8]
            jump_times = (self.when_to_start - 7) // 5
            try:
                if jump_times:
                    # 这里是跳转到场地页面
                    for i in range(jump_times):
                        self.driver.find_by_xpath("//*[@id='scrollTable']/div/table/thead/tr/td[6]/div/span").click()
                    # 这里是选定场地
                    for i in range(self.during_time):
                        (self.driver.find_by_xpath("//*[@id='scrollTable']/div/table/tbody/tr[%s]/td[%s]" % (
                        self.which_court, ((self.when_to_start - 7) % 5) + 1 + i))).click()
                else:
                    for i in range(self.during_time):
                        (self.driver.find_by_xpath("//*[@id='scrollTable']/div/table/tbody/tr[%s]/td[%s]" % (
                        self.which_court, ((self.when_to_start - 7) % 5) + 1 + i))).click()
            except Exception as e:
                print("this is reason:",e)
        #     此时已经选定好场地了
            self.driver.find_by_xpath('/html/body/div[1]/div/div/div[3]/div[2]/div/div[2]/div[4]/label/span').click()
            self.driver.find_by_xpath('/html/body/div[1]/div/div/div[3]/div[2]/div/div[2]/div[5]/div/div[2]').click()
            sleep(0.5)
            # 你需要提前在我的同伴中加入一个工具人,请一定要至少加一个人
            # /html/body/div[1]/div/div/div[3]/div[2]/div/div[2]/form/div/div[2]/div/div/label[1]
            self.driver.find_by_xpath('/html/body/div[1]/div/div/div[3]/div[2]/div/div[2]/form/div/div[2]/div/div/label[1]').click()
            self.driver.find_by_xpath('/html/body/div[1]/div/div/div[3]/div[2]/div/div[2]/div/div/div[2]').click()
            print("恭喜你，执行到这一步说明订单提交成功，请在10min内去个人中心-场馆预约-预约记录中进行支付！！！")

        else:
            print("登陆失败")











if __name__ == '__main__':
    username="********"
    password="********"
    # 想订哪一块场地
    which_court=int(input("要预定哪块场地：请输入1-8中的整数"))
    #想从几点开始打，24小时计，输入int
    when_to_start=int(input("想要什么时候打：请以24小时算，比如我想14：00打球，输入14即可，现在请输入一个整数"))
    # 要定几个小时，int,开馆时间是8-22，不要超过范围，会报错
    during_time=int(input("你想打几个小时，请输入一个整数"))
    Reserve_court(username,password,which_court,when_to_start,during_time).start_reserve()

